const deep = (data, prevent) => {
    let arr = [];
    data.forEach((item) => {
        if (!Array.isArray(item.children)) {
            item.component = require(`@/views${item.path}/index.vue`).default;
            $nuxt.$router.addRoute(prevent, item)
            arr.push(item);
        } else {
            item.children = deep(item.children, item.path);
            $nuxt.$router.addRoute(prevent, item)
            arr.push(item);
        }
    });

   

}


export default function(ctx) {
    //离开页面 刷新前 将store中的数据存到session
    window.addEventListener('beforeunload', () => {
        sessionStorage.setItem("storeCache", JSON.stringify(ctx.store.state))
    });
    //页面加载完成  将session中的store数据
    window.addEventListener('load', () => {

        let storeCache = sessionStorage.getItem("storeCache")
        if (storeCache) {
            deep(JSON.parse(storeCache).routeList, '')
            $nuxt.$router.addRoute('', {
                    path: 'error',
                    name: 404,
                    component: import ('../views/error.vue')

                })
                // 将session中的store数据替换到store中

        }
        ctx.store.replaceState(JSON.parse(storeCache));

    });
}